const { filePreview } = require('../../../api/index');
const { popularActivityCollectList, talentPolicyCollectList, friendshipActivityCollectList } = require('../../api/collect');

const {
  isFinished,
  sliceString,
  formatTime2
} = require('../../../utils/util');

const { getBatchDict } = require('../../../utils/dict');

// pages/collect/index.js
Page({
  data: {
    activityStateDicts: [],
    //  tab 列表
    tabs: [
      { name: '热门活动', code: '热门活动' },
      { name: '人才补贴', code: '人才补贴' },
      { name: '联谊会', code: '联谊会' }
    ],
    curTab: '热门活动',
    page: {
      pageNumber: 1,
      pageSize: 20
    },
    finished: false,
    //  人才补贴
    list: []
  },
  //  切换 tab 发送请求
  onChangeTabs(event) {
    const { code } = event.detail;

    this.setData({ curTab: code }, () => {
      this.data.page.pageNumber = 1;
      this.data.finished = false;
      this.setData({ list: [] }, () => {
        fecthList.call(this);
      })
    });
  },
  onRefresh() {
    this.data.finished = false;
    this.data.page.pageNumber = 1;
    this.setData({ list: [] }, () => {
      fecthList.call(this);
    })
  },
  loadMore() {  //  加载更多
    fecthList.call(this);
  },
  /*** 生命周期函数--监听页面加载 */
  async onLoad(options) {
    await fetchDicts.call(this);
    fecthList.call(this);
  }
})


//  获取列表数据
async function fecthList() {
  switch(this.data.curTab) {
    case '热门活动':
      fetchDataList.call(this, popularActivityCollectList, setResult1);
      break;
    case '人才补贴':
      fetchDataList.call(this, talentPolicyCollectList, setResult2);
      break;
    case '联谊会':
      fetchDataList.call(this, friendshipActivityCollectList, setResult3.bind(this));
  }
}

//  获取当前活动热门活动列表
async function fetchDataList(func, setResult) {
  wx.showLoading({
    title: '加载中...'
  });
  const { errors, result, totalCount } = await func(this.data.page);

  if (errors.lenght > 0) {
    return;
  }

  //  判断数据是不是都已经加载完了
  this.setData({
    finished: isFinished(totalCount, this.data.page.pageNumber * this.data.page.pageSize)
  });
  //  加载更多数据
  this.setData({
    list: this.data.list.concat(setResult(result))
  });
  this.data.page.pageNumber++;
  wx.hideLoading();
}

//  处理数据
function setResult1( data = [] ) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.activityPhoto = filePreview( '/file/preview/' + item.activityPhoto );
    item.createTime = formatTime2(item.createTime);
    item.subtitle = sliceString(item.subtitle || '', 14);
    return item;
  })

  return _data;
}

function setResult2( data = [] ) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.activityPhoto = filePreview( '/file/preview/' + item.activityPhoto );
    item.summary = sliceString( item.summary, 22 );
    item.publishTime = formatTime2(item.publishTime);
    return item;
  })

  return _data;
}

function setResult3( data = [] ) {
  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.activityBeginTime = formatTime2(item.activityBeginTime);
    item.activityEndTime = formatTime2(item.activityEndTime); 
    item.registrationBeginTime = formatTime2(item.registrationBeginTime);
    item.registrationEndTime = formatTime2(item.registrationEndTime);
    item.name = this.data.activityStateDicts.find(_s => _s.code === item.activityState)?.name;
    item.status = 'status-' + item.activityState;
    return item;
  })

  return _data;
}


//  获取所需字典项
async function fetchDicts() {
  const [ activityStateDicts ] = await getBatchDict(['ACTIVITY_STATE'])
  this.data.activityStateDicts = activityStateDicts;
}
